<?php

namespace Admin\Controller;


class TaskController extends AdminController {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $cpage = ((int)$_GET['p']) > 0 ? (int)$_GET['p'] : 1;
        $nickname = I("nickname",'','addslashes,trim');
        $status = I('status',0,'intval');

        if(empty($this->users)){
            $this->getAllUsers();
        }
        $user_id = 0;
        foreach ($this->users as $user){
            if($user['mobile'] == $nickname){
                $user_id = $user['id'];
                break;
            }
        }

        $where = [];
        if(!empty($nickname)){
            $where['user_id'] = $user_id;
        }

        if($status == 1){
            $where['status'] = 1;
        }elseif($status == -1){
            $where['status'] = 0;
        }

        $Store = M('IntegralTask');
        $count = $Store->where($where)->count();
        $Page = new \Think\Page($count, $this->page_size);
        $result = $Store->where($where)->order('id DESC')->page($cpage, $this->page_size)->select();
        $this->assign([
            'page' => $Page->show(),
            'result' => $result,
            'users' => $this->users,
        ]);
        $this->display();
    }

    public function delete(){
        $id = I('id', 0,'intval');
        if(empty($id)){
            return $this->error('缺少参数');
        }

        if(M("IntegralTask")->delete($id) !== false){
            return $this->success('删除成功');
        }

        return $this->error('删除失败');
    }
}